<template>
	<!-- 列表 -->
	<scroll-view
	  scroll-y="true"
	  enable-back-to-top
	  @scrolltolower="loadMore"
	  class="scroll-box"
	  :style="{ height: pageHeight + 'px' }"
	>
	<view v-for="(item,index) in orderLists" :key='item.id' 
	@click="navTo('groupBenefits/groupDetails?id='+item.bao_id+'&team='+GroupID+'&fxID='+user_id+'&type='+item.is_buy+'&status='+item.bao_status)">
		<view class="package" style="margin-bottom: 0px;padding-bottom: 0px;">
			<view class="text">{{item.title}}</view>
			<view class="space-between" style="width: 100%;">
				<view class="center polp">
					<image :src="item.fx_avatar" mode=""></image>
					<view>
						<text>{{item.fx_nickname}}</text>
						<text>分享</text>
					</view>
				</view>
				<view class="pay"  @click.stop="Onekey(item.bao_id)" v-if="item.bao_status=='2'">一键买同款</view>
			</view>
			
			<shopList :type="'subsidy'" :shoppAll="item.goods_list"></shopList>
		</view>
		<view class="package lists" style="margin-top: 0px;padding: 15rpx;">
			 <view class="space-between price">
				 <view>总价</view>
				 <view><text>¥</text><text>{{item.total_money}}</text></view>
			 </view>
			 <view class="title">按照车友团等级享该等级的平台补贴</view>
			  <view class="title">商品3件可以享受平台补贴</view>
			 <TeamPrice :state="'0'" :priceList="item.price_list" :addNums="'0'"></TeamPrice>
			 <view class="firstGroup">
				<text>我的最高等级的车友团</text>
				 <text>{{item.team_name}}</text>
				 <image :src="item.team_image" mode=""></image>
			 </view>
			 <view class="space-between price">
				 <view>平台补贴</view>
				 <view><text>-¥</text><text>{{item.yh_money}}</text></view>
			 </view>
			 <view class="space-between price">
				 <view style="color:#F54B39">我的团惠价</view>
				 <view><text>¥</text><text>{{item.pay_fee}}</text></view>
			 </view>
		</view>
	</view>
	</scroll-view>
	<!-- 新建团惠包弹框 -->
	<uni-popup ref="PobulicePopup" type="center" :is-mask-click="false">
	 <pobulice :title="'温馨提示'" :text="text" @close="closeBankPad" @submit="submit" :type="'center'"></pobulice>
	</uni-popup>
</template>

<script setup lang="ts">
import { navTo } from '@/utils/navigator';
import { ref } from 'vue';
import { onLoad, onShow } from '@dcloudio/uni-app';
import shopList from '@c/order/shopList.vue';
import TeamPrice from '@c/groupBenefits/teamPrice.vue';
import { toPublish } from '@mqtt';
import { errorToast, successToast,showLoading } from '@/utils/prompt';
import { numsTeam } from '@/gql/group';
import { teamGroup } from '@/gql/group';
import { getDriverID } from '@/stores/driverID';
const user_id = getDriverID();
const pageHeight = ref();
const GroupID = ref('');

const isLoading = ref(false);
const lastPage = ref(1);
const currentPage = ref(1);
const orderLists = ref([]);

const groupList = ref();
const borId = ref('');

const repeat = ref(true); //放重复点击

const PobulicePopup = ref(null);
const text = '您当前有正在创建新的团惠包  如果继续您新的团惠包内容将被覆盖';
onLoad((pearm) => {
  uni.getSystemInfo({
    success: function(res) {
      pageHeight.value = (res.windowHeight - 220).toString();
    },
  });
  GroupID.value = pearm.id;
  init();
});
onShow(() => {
  cartTeams();
});
function init() {
  isLoading.value = true;
  const payload = {
    query: teamGroup,
    variables: {
      page: currentPage.value,
      user_id,
      team_id: Number(GroupID.value),
    },
  };
  toPublish(
    'ql/store/getScoreGoodsList',
    payload,
    (obj: any) => {
      const { team_bao_list } = obj.data;
      isLoading.value = false;
      orderLists.value = [...orderLists.value, ...team_bao_list.list];
      lastPage.value = team_bao_list.lastpage;
    },
    () => {
      errorToast('通讯异常');
      isLoading.value = false;
    },
  );
}
/**
 * 加载更多
 */
function loadMore() {
  if (currentPage.value < lastPage.value) {
    currentPage.value++;
    init();
  }
}
/**
 * 获取团惠包数量
 */
function cartTeams() {
  const payload = {
    query: numsTeam,
    variables: {
      user_id,
    },
  };
  toPublish('ql/store/getCartTeamList', payload, (obj: any) => {
    const { team_shopping_nums } = obj.data;
    groupList.value = team_shopping_nums.nums;
  });
}
function Onekey(id) {
  borId.value = id;
  if (groupList.value < 1) {
    addGroup();
  } else {
    openBankPad();
  }
}
function submit() {
	showLoading()
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
  };
  toPublish('store/clearTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    closeBankPad();
    if (code === 1) {
      repeat.value = true;
      addGroup();
    } else {
      repeat.value = true;
      errorToast(msg);
    }
  });
}
function addGroup() {
		showLoading()
  if (!repeat.value) {
    return;
  }
  repeat.value = false;
  const payload = {
    user_id,
    bao_id: Number(borId.value),
  };
  toPublish('store/addTeamCart', payload, (obj: any) => {
    const { code, msg } = obj;
    if (code === 1) {
		repeat.value = true;
      successToast(msg);
      navTo('groupBenefits/groupEditor');
    } else {
			repeat.value = true;
      errorToast(msg);
    }
  });
}
function openBankPad() {
  PobulicePopup.value.open();
}
function closeBankPad() {
  PobulicePopup.value.close();
}
</script>

<style scoped lang="less">
@import url('../team/less/groupBao.less');
</style>